$(function() {
    $("#supply tbody tr td select.product").change(function() {
        var my = $(this);
        var key = eval(my.val());
        var nextId = my.next().attr("id");
        if (my.next().length == 0) {
            return true;
        }
        var data = my.data("products");
        if (data == undefined) {
            var json = jQuery.parseJSON(product);
            my.data("products", json);
            data = json;
        }
        try {
            var next = my.next();
            while (next.length != 0) {
                dwr.util.removeAllOptions(next.attr("id"));
                next = next.next();
            }
        } catch(e) {
        }
        $.map(data, function(o) {
            if (o.id == key) {
                dwr.util.addOptions(nextId, o.children, 'id', 'name');
                my.next().data("products", o.children);
                var value = my.next().attr("accesskey");
                var index = 0;
                for (var i = 0; i < o.children.length; i++) {
                    if (o.children[i].id == value) {
                        index = i;
                        break;
                    }
                }
                my.next().val(o.children[index].id);
            }
        });
        my.next().trigger("change");
    });
    $("#supply tbody tr td select.product:first").each(function() {
        var value = $(this).attr("accesskey");
        if (0 == value) {
            value = $(this).data("products")[0].id;
        }
        $(this).val(value);
        $(this).trigger("change");
    });
    $("#supply tbody tr td select.product:last").change(function() {
        $(".more").remove();
        clientInterface.fetchProperties($(this).val(), function(data) {
            data = eval(data);
            var length = data.length;
            var more = $("#more");
            for (var i = 0; i < length; i++) {
                var option = data[i];
                more.before("<tr class='more'><td align='right'>" + option.displayName + "：</td><td>" + convertElement(option) + "</td></tr>");
                var eleId = "more" + option.id;
                dwr.util.addOptions(eleId, option.options, 'id', 'name');
                if (option.editable) {
                    $("#" + eleId).append(otherOption);
                }
            }
        });
    });
    $(".area").change(function() {
        try {
            var key = eval($(this).val());
            var next = $(this).next();
            if (next.length == 0) {
                return true;
            }
            dwr.util.removeAllOptions(next.attr("id"));
            var json = jQuery.parseJSON(city);
            dwr.util.addOptions(next.attr("id"), json[key], 'id', 'name');
        } catch(e) {
        }
    });
    $(".area:first").each(function(i) {
        var value = $(this).attr("accesskey");
        if (0 != value) {
            $(this).val(value);
        }
        $(this).trigger("change");
    });
    $("#supply tbody tr td select.product:last").trigger("change");
});
var otherOption = "<option value='0'>其他</option>";
function convertElement(option) {
    if ("SELECT".equals(option.type)) {
        return "<select name='more' id='more" + option.id + "'></select>";
    } else if ("INPUT".equals(option.type)) {
        return "<input type='text' id='more" + option.id + "' name='more'/> ";
    }
}